package com.kbt.activity;


/**
 * @author zhgk
 * @package: com.kbt.activity
 * @TODO: 展示物流路径的页面Acvivity
 * @data: 2015/8/21 9:59
 * @version: V1.0
 */

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.kbt.adapter.GoodRoadInfoAdapter;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.model.data.*;
import com.kbt.net.RequestManager;
import com.kbt.util.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class GoodRoadInfoActivity extends Activity implements RequestManager.ResponseInterface{

    private ExpandableListView expandlistView;
    private GoodRoadInfoAdapter adapter;
    private List<GoodRoadInfoItem> goodRoadInfoItemList;
    private TextView textView77;//公司名
    private TextView textView79;//快递单号
    private TextView textView78;//运费
    private KuaiDiListBean kuaiDiListBean;
    private List<KuaiDiBean> kuaiDiBeans = new ArrayList<KuaiDiBean>();
    private RequestManager mRequestManager;
    private Map<String, Object> map =null;
    private String bm;
    private  String dh;
    private String kd_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.good_road_info);
        expandlistView = (ExpandableListView) findViewById(R.id.expandlist);

        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_return_btn);

        //设置返回按钮事件,关闭当前Activity，返回数据
        ImageView returnImg = (ImageView) findViewById(R.id.return_btn);
        returnImg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //不创建地址
                setResult(RESULT_CANCELED, null);
                BaseApplication.getInstance().removeActivity(GoodRoadInfoActivity.this);
                finish();
            }
        });

        TextView appTitleView = (TextView) findViewById(R.id.center_title);
        appTitleView.setText("跟踪订单");
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, Object>();
        bm = getIntent().getStringExtra("bm");
        dh = getIntent().getStringExtra("dh");
        kd_name = getIntent().getStringExtra("kd_name");
        initExpandListView();
        initData();
        BaseApplication.getInstance().addActivity(GoodRoadInfoActivity.this);
    }

    /**
     * 初始化可拓展列表
     */
    private void initExpandListView() {
        textView77 = (TextView) findViewById(R.id.textView77);
        textView79 = (TextView) findViewById(R.id.textView79);
        textView78 = (TextView) findViewById(R.id.textView78);
//        adapter = new GoodRoadInfoAdapter(this, kuaiDiBeans);
//        expandlistView.setAdapter(adapter);
        expandlistView.setGroupIndicator(null); // 去掉默认带的箭头
    }
    //初始化数据
    private void initData(){
        if(Utils.isNetworkAvailable(GoodRoadInfoActivity.this)) {
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.kuaiDiURL + "?kd_bm=" + bm + "&kd_dh=" + dh, Constants.RequestTag.kuaiDiTag, map, KuaiDiListBean.class);
        }else{
            Toast.makeText(GoodRoadInfoActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        if (tag == Constants.RequestTag.kuaiDiTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                kuaiDiListBean = (KuaiDiListBean) parsedGSON;
                if (kuaiDiListBean.getResult().equals("success")) {
                    textView77.setText(kd_name+"");
                    textView79.setText(dh);
                    if(kuaiDiListBean.getData().getData()!=null) {
                        if (kuaiDiListBean.getData().getData().size() > 0) {
                            kuaiDiBeans = kuaiDiListBean.getData().getData();
//                            adapter.setList(kuaiDiBeans);
//                            adapter.notifyDataSetChanged();
                        }
                    }else{
                        kuaiDiBeans.clear();
                        KuaiDiBean kuaiDiBean=null;
                        for(int i=0;i<1;i++){
                            kuaiDiBean =new  KuaiDiBean();
                            kuaiDiBean.setContext("暂时没有物流信息!");
                            kuaiDiBean.setTime("");
                            kuaiDiBeans.add(kuaiDiBean);
                        }
//                        adapter.setList(kuaiDiBeans);
//                        adapter.notifyDataSetChanged();
                    }
                }else{
                    Toast.makeText(GoodRoadInfoActivity.this,kuaiDiListBean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        Toast.makeText(GoodRoadInfoActivity.this,R.string.noWangluo,Toast.LENGTH_SHORT).show();
    }
}

